/*
 * UpdateMeetingService.java
 *
 * Created on 07 March 2006, 16:46
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package web.services;
//import business.pojos.Meeting;
import java.util.Calendar;
import se392.ateam2006.meetingmgmt.meeting.MeetingEntity;
import se392.ateam2006.resourcemgmt.room.RoomEntity;
//import business.pojos.Room;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import se392.ateam2006.meetingmgmt.meeting.Duration;
import web.util.BusinessFacadeDelegate;
import web.util.TimeFormat;

/**
 *
 * @author Phil
 */
public class UpdateMeetingService extends UpdateService {
    
    /** Creates a new instance of UpdateMeetingService */
    public UpdateMeetingService() {
    }

    public String executePreDispatch(HttpServletRequest request,
            ServletContext context)
            throws Exception {
        BusinessFacadeDelegate delegate = this.getDelegate(request.getSession());
        
        String uid = request.getParameter(MEETING_ID);
        String title = request.getParameter(MEETING_TITLE);
        String description = request.getParameter(MEETING_DESCRIPTION);
        Date startDate = TimeFormat.parseDate(request.getParameter(MEETING_START_DATE));
        GregorianCalendar startDateRange = new GregorianCalendar();
        startDateRange.setTime(startDate);        
        Date endDate = TimeFormat.parseDate(request.getParameter(MEETING_END_DATE));
        GregorianCalendar endDateRange = new GregorianCalendar();
        endDateRange.setTime(endDate); 
        String durationString = request.getParameter(MEETING_DURATION_DAY) + " " + request.getParameter(MEETING_DURATION_HOUR) + ":" + request.getParameter(MEETING_DURATION_MIN);
        Duration duration = TimeFormat.parseTime(durationString);
        
        int roomSize = -1;
        String roomSizeAsString = request.getParameter(MEETING_ROOM_SIZE);
        if (null != roomSizeAsString && !(roomSizeAsString.equals(""))) {
            roomSize = Integer.parseInt(roomSizeAsString);
        }
        String venue = request.getParameter(MEETING_VENUE);
        //TODO add in room location and owner
        RoomEntity room = null;
        if (null != venue && !(venue.equals(""))) {
            room = new RoomEntity(venue, roomSize, null, null);
        }
        
        //UserEntity initiator, String id, String title, String description,
        //              Calendar startDateRange, Calendar endDateRange,
        //              Duration duration, String equipment
        
       // UserEntity initiator, String id, String title, String description,
       //               Calendar startDateRange, Calendar endDateRange,
       //               Duration duration, int requestedRoomSize, String equipment, RoomEntity room
                         
        MeetingEntity meeting = delegate.createMeeting(delegate.getLoggedInUser(), uid, title, description,  startDateRange, endDateRange, duration, roomSize, "", room);
        String bob ="bob";
        request.getSession().setAttribute(Service.ATTRIBUTE_MEETING, meeting);
        request.getSession().setAttribute("muid", uid);
        return "display";
        
    }
    
    public void executePostDispatch(HttpServletRequest request,
            ServletContext context)
            throws Exception {
        request.getSession().setAttribute(Service.ATTRIBUTE_MEETING, null);
    }
}
